<?php
/**
 * @author: liaofei<136327134@qq.com>
 * @day: 2021/6/8
 */

namespace App\Services\user;

use App\Dao\user\UserAgentDao;

use App\Models\system\SystemAdminModel;
use App\Services\BaseServices;
use Hlcreation\Basic\Helper;
use App\Exceptions\ApiException as HOException;
use App\Dao\system\SystemAdminDao;
use App\Services\system\SystemLogServices;
use Illuminate\Support\Facades\DB;
/**
 * Class UserAgentServices
 * @package app\services\user
 */
class UserAgentServices extends BaseServices
{



    /**
     * UserAgentServices constructor.
     * @param UserAgentDao $dao
     */
    public function __construct(UserAgentDao $dao)
    {
        $this->dao = $dao;
    }


    /**
     * 代理商列表
     * @param string $account
     */
    public function AgentList(array $where,$post)
    {

        return  $this->dao->AgentList($where,$post);

    }


    /**
     * 组合数据
     */
    private function  CombinedData(array $data=[],string $str=''){

        //账号信息
        $user['created_at'] =date('Y-m-d H:i:s');
        $user['expiration_time']= empty($data['expiration_time'])?0:strtotime($data['expiration_time']);
        $user['account']=$data['account'];
        $user['halt']=Helper::createNonceStr(5);
        $user['pwd']=Helper::returnPwd($data['password'], $user['halt']);
        $user['roles']=3;
        $user['level']=2;
        $user['status']=1;
        $user['creator_uid']=$data['creator_uid'];

        $user['it_disabled']=0;
        $user['store_num']=$data['store_num'];
        $user['real_name']=$data['agent_name'];
        //代理商信息
        $agent['created_at']=date('Y-m-d H:i:s');
        $agent['contacts_name']=$data['agent_name'];
        $agent['danwei']=$data['company_name'];
        $agent['tel']=$data['phone'];
        $agent['danwei']=$data['company_name'];

        return compact('user','agent');
    }


    /**
     * 创建代理商
     */
    public  function  AgentSave(array $arr){
        $systemAdminServices = app()->make(SystemAdminDao::class);
        if($systemAdminServices->count(['account'=>$arr['account']])) throw new HOException('账号已存在');
        if(empty($arr['real_name']))  throw new HOException('真实姓名不能为空');
        //$data=$this->CombinedData($arr,'AgentSave');
        $arr['level'] = 2;  //代理商

        return  $this->dao->AgentSave($arr);



    }


    /**
     * 修改代理商
     */
    public  function   AgentUpdate(string $agent_id,array $arr){
        if($agent_id=='') throw new HOException('代理商id不能为空');

        if (empty($arr['pwd'])){
            $arr['pwd'] = SystemAdminModel::where('id',$agent_id)->value('pwd');
        }
        $systemAdminServices = app()->make(SystemAdminDao::class);
        // if($systemAdminServices->IsAccount($arr['account'],$agent_id)) throw new HOException('账号已存在');
        /* $data=$this->CombinedData($arr,'AgentUpdate');*/
        return  $this->dao->AgentUpdate($agent_id,$arr);
    }


    /**
     * 获取代理商信息
     */
    public  function  getAgent(string $agent_id=''){
        if($agent_id=='') throw new HOException('代理商id不能为空');
        return  $this->dao->getAgent(['id'=>$agent_id]);

    }

    /**
     * 软删除
     */
    public  function  agentDelte(int $admin_id,string $agent_id){
        if($agent_id=='') throw new HOException('代理商id或账号id不能为空');
        return  $this->dao->agentDelte($admin_id,$agent_id,);

    }


    /**
     * 禁用代理商
     */
    public  function  editDisabled($admin_id,string $user_id,int $tatus){
        if($user_id=='') throw new HOException('账号id不能为空');
        $systemAdminServices = app()->make(SystemAdminDao::class);

        if($systemAdminServices->update(['id'=>$user_id],['it_disabled'=>$tatus])){
            //插入日志
            $type=empty($tatus)?'关闭代理商':'禁用代理商';
            $LogServices = app()->make(SystemLogServices::class);
            $LogServices->SaveLog($admin_id,'禁用代理商','代理商账号id:'.$user_id.'操作:'.$tatus);
            return true;
        }else{
            return false;
        }



    }

    /**
     * 代理商配置开关
     */
    public  function  agentConfig(int $admin_id,int $type,int $id, int $status){
        if(empty($id))  throw new HOException('代理商不存在');
        $systemAdminServices = app()->make(SystemAdminDao::class);
        switch ($type) {
            case 1:
                if($systemAdminServices->update(['id'=>$id],['oem_status'=>$status])){
                    //插入日志
                    $type=empty($status)?'开启oem配置项':'禁用oem配置项';
                    $LogServices = app()->make(SystemLogServices::class);
                    $LogServices->SaveLog($admin_id,'代理商配置开关','代理商账号id:'.$id.'操作:'.$type);
                    return true;
                }else{
                    return false;
                }

                break;
            case 2:
                if($systemAdminServices->update(['id'=>$id],['cloud_status'=>$status])){
                    //插入日志
                    $type=empty($status)?'开启云储存配置项':'禁用云储存配置项';
                    $LogServices = app()->make(SystemLogServices::class);
                    $LogServices->SaveLog($admin_id,'代理商配置开关','代理商账号id:'.$id.'操作:'.$type);
                    return true;
                }else{
                    return false;
                }
                break;
            case 3:
                if($systemAdminServices->update(['id'=>$id],['sms_status'=>$status])){
                    //插入日志
                    $type=empty($status)?'开启短信配置项':'禁用短信配置项';
                    $LogServices = app()->make(SystemLogServices::class);
                    $LogServices->SaveLog($admin_id,'代理商配置开关','代理商账号id:'.$id.'操作:'.$type);
                    return true;
                }else{
                    return false;
                }
                break;
        }


    }

    /**
     * 修改代理商短信次数合同次数
     * @param int $agent_id
     * @param int $admin_id
     * @param array $arr
     */
    public  function  setAgentSmsContract(int $agent_id ,$admin_id,array $arr){
        DB::beginTransaction();
        try{
            $AdminServices = app()->make(SystemAdminDao::class);
            $AdminServices->update(['id'=>$agent_id],['store_num'=>$arr['store_num'],'shop_num'=>$arr['shop_num'],'sms_num'=>$arr['sms_num']]);
            $LogServices = app()->make(SystemLogServices::class);
            $LogServices->SaveLog($admin_id,'代理商修改短信次数合同次数','代理商账号ID:'.$agent_id.'修改短信数量:'.$arr['sms_num'].'---修改店铺数量:'.$arr['shop_num'].'---修改平台数量:'.$arr['store_num']);
            DB::commit();
            return true;
        } catch(\Exception $exception) {
            DB::rollBack();
            throw new HOException($exception->getMessage());

        }
    }


}
